<template>
  <a-row type="flex" justify="center">
    <a-col :span="22">
      <a-card title="号码屏蔽池">
        <div class="table-page-search-wrapper">
          <a-form-model
            layout="inline"
            :model="queryParam"
          >
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属公司">
                  <a-select v-model="queryParam['filter[company]']" defaultValue="all">
                    <a-select-option v-for="(item,index) in companyOpts2" :key="index" :value="index">
                      {{item}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务名称">
                  <a-input v-model="queryParam['filter[b_name]']"/>
                </a-form-model-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-item label="入库时间">
                    <a-range-picker @change="onTime1" />
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="领取时间">
                    <a-range-picker @change="onTime2" />
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="业务分类">
                    <a-select v-model="queryParam['filter[first_type]']" :default-value="0">
                      <a-select-option v-for="(item,index) in firstTypeOpts" :key="index" :value="index">
                        {{item}}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="搜索引擎">
                    <a-input v-model="queryParam['filter[search_engine]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="首次页面">
                    <a-input v-model="queryParam['filter[first_url]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="按搜索词">
                    <a-input v-model="queryParam['filter[search_keywords]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="领取顾问">
                    <a-input v-model="queryParam['filter[receiver_info]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索的IP">
                    <a-input v-model="queryParam['filter[ip]']"/>
                  </a-form-item>
                </a-col>
              </template>
              <a-col :md="!advanced && 8 || 24" :sm="24">
                  <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                    <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                    <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                      {{ advanced ? '收起' : '展开' }}
                      <a-icon :type="advanced ? 'up' : 'down'" />
                    </a>
                  </span>
              </a-col>
            </a-row>
          </a-form-model>
        </div>
        <div class="table-operator">
          <s-table
            bordered
            ref="table"
            :row-key="(record) => record.customer_id"
            :columns="customerColumns"
            :data="customerData"
          >
            <template slot="title">
              <a-form-model
                ref="numForm"
                layout="inline"
                :model="numForm"
                :rules="numRules"
              >
                <a-form-model-item>
                  <a-input type="text" v-model="numForm.shield_segment_no" placeholder="输入号段，分号隔开"></a-input>
                </a-form-model-item>
                <a-form-model-item>
                  <a-button type="danger" icon="plus" @click="submitNo">添加号段</a-button>
                </a-form-model-item>
              </a-form-model>
            </template>
            <template slot="company" slot-scope="text">
              {{ companyOpts2[text] }}
            </template>
            <template slot='ip' slot-scope='text,record'>
              {{record.device}}<br>{{text}}<br>{{record.ip_format}}
            </template>
            <template slot="search_keywords" slot-scope="text,record">
              <div>{{text}}</div>
              <div>{{record.search_engine}}</div>
            </template>
            <template slot='first_url' slot-scope='text'>
              <a :href='text' target='_blank'>{{text}}</a>
            </template>
          </s-table>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { STable } from '@/components'
  import customerColumns from '@/columns/customer'
  import { getAddNo, getScreenList, postAddNo } from '@/api/customer'
  export default {
    components: {
      STable
    },
    name: 'ScreenPool',
    data () {
      return {
        customerColumns: customerColumns.all_list3,
        customerData: parameters => {
          return getScreenList(Object.assign(parameters, this.queryParam)).then(res => {
            this.companyOpts2 = res.result.company_opts
            this.firstTypeOpts = res.result.first_type_opts
            return res.result
          })
        },
        queryParam: {},
        advanced: true,
        companyOpts2: {},
        firstTypeOpts: {},
        visible: false,
        companyOpts: {},
        numForm: {},
        numRules: {
          shield_segment_no: [{ required: true, message: '必须输入号码段', trigger: 'blur' }]
        }
      }
    },
    created () {
      this.getNo()
    },
    methods: {
      getNo () {
        getAddNo().then(res => {
          if (res.status === 0) {
            this.numForm.shield_segment_no = res.result.setting.shield_segment_no
          }
        })
      },
      submitNo () {
        const app = this
        const data = {}
        app.$refs.numForm.validate(valid => {
          if (valid) {
            data['setting[shield_segment_no]'] = app.numForm.shield_segment_no
            postAddNo(data).then(res => {
              if (res.status === 0) {
                app.getNo()
                app.$refs.table.refresh(true)
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          } else {
            return false
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onTime1 (date, dateString) {
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      onTime2 (date, dateString) {
        this.queryParam['filter[received_at_start]'] = dateString[0]
        this.queryParam['filter[received_at_end]'] = dateString[1]
      },
      resetForm () {
        const app = this
        app.queryParam = {}
        app.$refs.table.refresh(true)
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
    }
  }
</script>

<style scoped>

</style>
